import { defineStore } from 'pinia'
import { matchList, memberGptToken } from '@/api/calc'
import { startTime, convertSeconds } from '@/utils/helper'
import { sum } from 'lodash-es'

export const useArtStore = defineStore('art', {
  persist: {
    key: 'art',
    omit: ['content_types', 'checked', 'token'], // 遗忘的项
    // pick: ['checked']
  },
  state: () => ({
    checked: false,
    content_types: null,
    matchList: [],
    token: null,
  }),
  actions: {
    async getWsToken(){
      if (this.token) return;
      try {
        const res = await memberGptToken();
        if (res) {
          this.token = res;
        }
      } catch (error) {
        console.log('err', error)
      }
    },
    setContentTypes(data){
      if(!data) return;
      this.content_types = data;
    },
    clearMatchList(){
      this.matchList = [];
    },
    updateMatchList(val){
      const { id, score } = val;
      const findIdx = this.matchList.findIndex(item => item.m_id == id);
      if (findIdx>-1){
        this.matchList[findIdx].timestemp = score[4]; // 时间戳
        // 足球的时间是 score[4]  篮球是score[2]
        if (this.matchList[findIdx].types == 2){ //篮球
          this.matchList[findIdx].minute = convertSeconds(score[2]); // 篮球倒计时秒数转换
          this.matchList[findIdx].home_full = sum(score[3]);
          this.matchList[findIdx].guest_full = sum(score[4]);
        }else{
          this.matchList[findIdx].minute = startTime(score[1], score[4]); // 足球比赛开始分钟数
          this.matchList[findIdx].home_full = score[2][0];
          this.matchList[findIdx].home_half = score[2][1];
          this.matchList[findIdx].guest_full = score[3][0];
          this.matchList[findIdx].guest_half = score[3][1];
        }
        this.matchList[findIdx].match_status = score[1]; // score[1]  0比赛异常、1未开赛
      }
    },
    async queryMatchs(parame){
      try {
        const res = await matchList(parame);
        if(res){
          this.matchList = res.match_list;
          return this.matchList;
        }else{
          return [];
        }
      } catch (error) {
        throw error;
      }
    }
  }
})
